package com.neusoft.controller;

import com.neusoft.po.Credit;
import com.neusoft.service.CreditService;

import com.neusoft.po.CommonResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RefreshScope
@RestController
@RequestMapping("/Credit")
public class CreditController {

    @Autowired
    private CreditService creditService;

    //为一个用户建立一个积分项，设置0
    @PostMapping("/Credit/{userId}")
    public CommonResult<Integer> insertCredit(@PathVariable("userId") String userId) throws Exception {
        return new CommonResult<Integer>(200, "success", creditService.insertCredit(userId));
    }

    //通过用户ID获得所持有的积分总数
    @GetMapping("/TotalCredit/{userId}")
    public CommonResult<Integer> getTotalCreditByUserId(@PathVariable("userId") String userId) throws Exception {
        return new CommonResult<Integer>(200, "success", creditService.getTotalCreditByUserId(userId));
    }

    //使用积分，使得积分减少
    @PutMapping("/PayoutByUserId/{userId}/{sub_number}")
    public CommonResult<Integer> payoutByUserId(@PathVariable("userId")String userId,@PathVariable("sub_number") Integer sub_number) {
        int result=creditService.payoutByUserId(userId, sub_number);
        return new CommonResult(200,"success",result);
    }

    //增加积分
    @PutMapping("/RechargeByUserId/{userId}/{number}")
    public CommonResult<Integer> rechargeByUserId(@PathVariable("userId")String userId,@PathVariable("number")Integer number){
        int result = creditService.rechargeByUserId(userId,number);
        CommonResult commonResult = new CommonResult(200,"success",result);
        return commonResult;
    }

}
